package com.xiaotu.spring.config;

import com.xiaotu.spring.service.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.*;

/**
 * @author zhangxiao
 * @create 2021-11-09 14:23
 */

/**
 * @Import 中使用，需要注意的是第一种(BeanG)、第二种方式(MyImportSelector->①HanDanBeanA ②XinJiBeanA)在IOC容器中bean名称是类的全类名
 */
@Import({BeanG.class, MyImportSelector.class, MyImportBeanDefinitionRegistrar.class})
@Configuration(proxyBeanMethods = false)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass({BeanC.class})
@ConditionalOnMissingBean(BeanD.class)
@PropertySources(value = {@PropertySource(value = "classpath:test.properties")})
public class TestCustomAutoConfiguration {

    /**
     * EventListener 该组件优先于 EventSource 加载
     *
     * @return
     */
    @DependsOn({"eventListener"})
    @Bean
    public EventSource eventSource() {
        return new EventSource();
    }

    @Bean
    public EventListener eventListener() {
        return new EventListener();
    }

//    @Bean
//    public BeanE beanE1() {
//        return new BeanE();
//    }


    @Bean(initMethod = "init", destroyMethod = "destroy")
    /**
     * @ConditionalOnMissingBean 当不存在该 BeanE 的实例的时候加载本实例，避免相同类型的实例重复
     */
    @ConditionalOnMissingBean(BeanE.class)
    /**
     * @ConditionalOnProperty 例：x.xx=xxx
     * prefix：配置文件中key的前缀 x
     * name：配置文件中key的后缀 xx
     * havingValue：配置文件中value值 xxx
     */
    @ConditionalOnProperty(prefix = "testConditionalOnProperty", name = "conditionalOnProperty", havingValue = "false")
    public BeanE beanE() {
        return new BeanE();
    }

}
